package com.fl.common.valid;

import org.springframework.validation.annotation.Validated;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> sets = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (vals.length>0){
            for (int val : vals) {
                sets.add(val);
            }
        }

    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext Context) {

        return sets.contains(value);
    }
}
